package com.whh.zuowen.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whh.zuowen.count.event.ZuowenCountEvent;
import com.whh.zuowen.mapper.ZuowenMapper;
import com.whh.zuowen.pojo.Zuowen;
import com.whh.zuowen.service.ZuowenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ZuowenServiceImpl implements ZuowenService {
    @Autowired
    ZuowenMapper zuowenMapper;

    @Autowired
    ApplicationContext context;
    @Autowired
    ApplicationEventPublisher applicationEventPublisher;

    @Override
    public PageInfo<Zuowen> getList(int page, String grade, String type) {
        PageHelper.startPage(page, 15);
        PageInfo<Zuowen> zuowenPageInfo = new PageInfo<>(zuowenMapper.getList(grade, type));
        return zuowenPageInfo;
    }

    @Override
    public List<Zuowen> getHot() {
        return zuowenMapper.getHot();
    }

    @Override
    public Zuowen getRecord(Integer id) {
        context.publishEvent(new ZuowenCountEvent(this, id));
//        applicationEventPublisher.publishEvent(new ZuowenCountEvent(this, id));
        return zuowenMapper.getRecord(id);
    }

    @Override
    public int countPlus(Integer id) {
        return zuowenMapper.countPlus(id);
    }

    @Override
    public List<Zuowen> getTuijian(List<Integer> ids) {
        return zuowenMapper.getTuijian(ids);
    }
}
